package com.lyc.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.lyc.dto.PageDTO;

public class ELTools {

	private static ThreadLocal<SimpleDateFormat> format = ThreadLocal
			.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));

	public String formatDate(Date date) {
		return format.get().format(date);
	}

	public String formatDate(Date date, String format) {
		return new SimpleDateFormat(format).format(date);
	}

	public String formatNumber(BigDecimal number, int len) {
		number = number.setScale(len, RoundingMode.HALF_UP);
		return number.toString();
	}

	public String formatNumber(Float number, int len) {
		return formatNumber(new BigDecimal(number), len);
	}

	public String formatNumber(Double number, int len) {
		return formatNumber(new BigDecimal(number), len);
	}

	public String formatNumber(String number, int len) {
		return formatNumber(new BigDecimal(number), len);
	}

	public String formatNumber(double number, int len) {
		return formatNumber(new BigDecimal(number), len);
	}

	private String li = "<li class=\"page-item{active}\"><a class=\"page-link\" data-page=\"{pageNumber}\" href=\"{pageURL}\">{pageText}</a></li>";

	private String formatLi(boolean active, int pageNumber, String pageURL, String pageText) {
		return li.replace("{active}", active ? " active" : "").replace("{pageNumber}", pageNumber + "")
				.replace("{pageURL}", pageURL).replace("{pageText}", pageText);
	}

	public String page(PageDTO<?> page, int max, String url) {
		if (page == null || page.getTotal() == 0) {
			return null;
		}

		if (max < max) {
			max = 5;
		}
		if (url == null) {
			url = "javascript:void(0);";
		}
		StringBuffer html = new StringBuffer("<ul class=\"pagination justify-content-center\">");
		List<Integer> pages = page.getPages(max);
		if (pages.get(0) > 1) {
			html.append(formatLi(false, 1, url.replace("{page}", "1"), "首页"));
		}
		for (Integer i : pages) {
			html.append(formatLi(i.equals(page.getCurrent()), i, url.replace("{page}", i.toString()), i.toString()));
		}
		if (pages.get(pages.size() - 1) < page.getPageMax()) {
			html.append(formatLi(false, page.getPageMax(), url.replace("{page}", page.getPageMax().toString()), "尾页"));
		}
		html.append("</ul>");
		return html.toString();
	}

	public String page(PageDTO<?> page, int max) {
		return page(page, max, null);
	}

	public String page(PageDTO<?> page, String url) {
		return page(page, 5, url);
	}

	public String page(PageDTO<?> page) {
		return page(page, 5, null);
	}

	public String concat(String... strs) {
		return String.join("", strs);
	}

	public List<String> toList(String str) {
		try {
			return JSON.parseArray(str, String.class);
		} catch (Exception ex) {
			return null;
		}
	}

	public String subStr(String str, int maxLen, boolean removeHtml) {
		if (removeHtml) {
			str = str.replaceAll("<[^>]+>", "");
		}
		str = str.replaceAll("\\s", "");
		if (str.length() <= maxLen) {
			return str;
		} else {
			return str.substring(0, maxLen - 2) + "...";
		}
	}

}
